package arch.mem;

import java.util.Observable;

import util.SysHelper;

public final class MemoryControl extends Observable
{
	private static final int memorySize = 16;
	private static final int imemorySize = 16;
	private int[] Memory = new int[memorySize];
	private String[] iMemory = new String[imemorySize];
	private boolean isDmem;
	private boolean isImem;

	public MemoryControl()
	{
	}
	
	public int[] getDMem()
	{
		return this.Memory;
	}
	
	public String[] getIMem()
	{
		return this.iMemory;
	}

	public void reset()
	{
		Memory = new int[memorySize];
		iMemory = new String[imemorySize];
		isDmem = false;
		isImem = false;
		notifyUI();
	}

	public int get(int address)
	{
		isDmem = true;
		notifyUI();
		
		SysHelper.sleep();
		isDmem = false;
		notifyUI();
		return Memory[address];
	}

	public void set(int address, int value)
	{
		isDmem = true;
		notifyUI();
		
		SysHelper.sleep();
		Memory[address] = value;
		isDmem = false;
		notifyUI();
	}

	public String iget(int address)
	{
		isImem = true;
		notifyUI();
		
		SysHelper.sleep();
		isImem = false;
		notifyUI();
		return iMemory[address];
	}

	public void iset(int address, String instruction)
	{
		isImem = true;
		notifyUI();
		
		SysHelper.sleep();
		iMemory[address] = instruction;
		isImem = false;
		notifyUI();
	}
	
	public void notifyUI()
	{
		setChanged();
		notifyObservers(new Object[]{isImem, isDmem, iMemory, Memory});
	}
}